import React, { Component } from 'react'
import {View, Text,StatusBar,Image } from 'react-native'
import Storage from 'react-native-storage'
import AsyncStorage from '@react-native-community/async-storage';
import {inject, observer} from 'mobx-react'
let that;
const getState= async () => {
  //获取存储Token
    let  token = 0;
    try{
      token = await global.storage.load({
        key:'token'
      })
      //如果有则跳转到主页,并把token重新注入到mobx中
      // console.log(that.props)
      that.props.RootStore.updateToken(token)
      //跳转到home
      that.props.navigation.push('Home')
    }
    catch(err){
      //跳转到登陆注册的页面
      that.props.navigation.push('Login')
    }
   
} 
@inject('RootStore')
@observer
class Main extends Component {
  constructor(props) {
    super(props)
    this.state = {

    }
    that = this
  }
  componentDidMount(){
    var storage = new Storage({
      size: 1000,
      storageBackend: AsyncStorage,
      defaultExpires: null,
      enableCache: true,
  })
    // 全局变量
    global.storage = storage
    // 发送请求，查看本地是否存在token，如果存在，直接跳转到主页，不存在调转到注册
    // global.storage.save({
    //   key:'token',
    //   data: '777878',
    //   expires: null
    // });
    //  storage.remove({
    //    key: 'lastPage'
    //  });
    //
    setTimeout(() => {
      getState()
      // console.log(token)
    }, 1000);
   
    
  }
  render() {
    return (
      <View style={{flex:1}}>
        <Image source={{uri:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1605585365391&di=6c7fcf1b907fd9624659c9c3dd83f375&imgtype=0&src=http%3A%2F%2Fci.xiaohongshu.com%2F3c73c9bf-7851-4406-8b64-8bf469030ab7%40r_640w_640h.jpg'}}
          style={{height:'100%'}}
        ></Image>
        <Text style={{position:'absolute',left:'40%',fontSize:35,color:'white',top:"40%",fontStyle:'italic',fontWeight:'bold'}}>心相印</Text>
      </View>
    )
  } 
}
export default Main